package com.redteam.models;

import com.redteam.data.beans.Airplane;
import com.redteam.data.beans.Person;
import com.redteam.data.beans.Reservation;
import com.redteam.data.dao.AirplaneDAO;
import com.redteam.data.dao.ReservationDAO;
import com.redteam.util.TimeCalculator;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Teddy Karam
 */
public class Report {

    private ReservationDAO dao;

    /**
     * Default constructor
     */
    public Report() {
        dao = ReservationDAO.getInstance();
    }

    /**
     * Returns an array list of all reservations for a given item
     * @param rentalItemId
     * @return reservationList
     */
    public List<Reservation> getReservationListSingleItem(long rentalItemId) {
        List<Reservation> reservationList = new ArrayList<Reservation>();

        try {
            reservationList = dao.getAllforAnyItem(rentalItemId);
        } catch (SQLException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        }
        return reservationList;
    }

    /**
     * Returns an array list of all reservations for a given item within a time frame
     * @param rentalItemId
     * @param startDate
     * @param endDate
     * @return reservationByDate
     */
    public List<Reservation> getReservationListSingleItem(long rentalItemId, Date startDate, Date endDate) {
        List<Reservation> reservationByDate = new ArrayList<Reservation>();
        for (Reservation r : getReservationListSingleItem(rentalItemId)) {
            if (startDate.compareTo(r.getStartDate()) >= 0 && endDate.compareTo(r.getEndDate()) <= 0) {
                reservationByDate.add(r);
            }
        }
        return reservationByDate;
    }

    /**
     * Returns an array list of all the reservations
     * @return allReservations
     */
    public List<Reservation> getReservationListAllItems() {
        List<Reservation> allReservations = new ArrayList<Reservation>();
        try {
            allReservations = ArrayList.class.cast(dao.getAll());
        } catch (SQLException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        }

        return allReservations;
    }

    /**
     * Returns an array list of all the reservations for a given time frame
     * @param startDate
     * @param endDate
     * @return allReservationsByDate
     */
    public List<Reservation> getReservationListAllItems(Date startDate, Date endDate) {
        List<Reservation> allReservationsByDate = new ArrayList<Reservation>();
        for (Reservation r : getReservationListAllItems()) {
            if (startDate.compareTo(r.getStartDate()) >= 0 && endDate.compareTo(r.getEndDate()) <= 0) {
                allReservationsByDate.add(r);
            }
        }

        return allReservationsByDate;
    }

    /**
     * Returns an array list of all the reservation for given renter Id
     * @param renterId
     * @return reservationListPerPerson
     */
    public List<Reservation> getReservationListPerPersonID(long renterId) {
        List<Reservation> reservationListPerPerson = new ArrayList<Reservation>();

        try {
            reservationListPerPerson = dao.getAllforRenter(renterId);
        } catch (SQLException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        }
        return reservationListPerPerson;
    }

    /**
     * Returns an array list of all the reservation for given person
     * @param person
     * @return reservationListPerPerson
     */
    public List<Reservation> getReservationListPerPerson(Person person) {

        return getReservationListPerPersonID(person.getId());
    }

    /**
     * Returns a 2 dimension array of string containing the data needed for the Aircraft/Simulator rental history detail.
     * @param rentalItemId
     * @param startDate
     * @param endDate
     * @return historyDetail
     */
    public String[][] getAircraftRentalHistoryDetail(long rentalItemId, Date startDate, Date endDate) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = getReservationListSingleItem(rentalItemId, startDate, endDate);
        String[][] historyDetail = new String[11][reservationList.size()];
        int row = 0;
        for (Reservation r : reservationList) {
            historyDetail[0][row] = Long.toString(r.getId());       //reservation id

            historyDetail[1][row] = r.getDiscriminator();         //reservation type airplane or simulator

            historyDetail[2][row] = r.getRentalItem().getAircraftType().getAircraftType();
            historyDetail[3][row] = r.getReserver().getFirstName() + " " + r.getReserver().getLastName();// Renter's name and last name

            historyDetail[4][row] = r.getPilot().getFirstName() + " " + r.getPilot().getLastName();//Instructor or charter Pilot name

            historyDetail[5][row] = r.getReserver().getDiscriminator();     // discriminator

            historyDetail[6][row] = r.getStartDate().toString();//start date

            historyDetail[7][row] = r.getEndDate().toString();  //end date

            historyDetail[8][row] = (new TimeCalculator(r.getStartDate(), r.getEndDate(), r.getStartTime(), r.getEndTime())).toString(); //total amount of time


            historyDetail[9][row] = r.getAgreement();
            historyDetail[10][row] = Double.toString(r.getPrice());
            row++;


        }

        return historyDetail;
    }

    /**
     *Returns a 2 dimensions array containing all the rentals for a given time frame.
     * @param startDate
     * @param endDate
     * @return historySummary
     */
    public String[][] getAllRentalHistorySummary(Date startDate, Date endDate) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = getReservationListAllItems(startDate, endDate);
        String[][] historySummary = new String[10][reservationList.size()];
        int row = 0;
        for (Reservation r : reservationList) {
            historySummary[0][row] = Long.toString(r.getId());          //reservation id
            historySummary[1][row] = r.getRentalItem().getType();               // reservation type
            historySummary[2][row] = r.getRentalItem().getAircraftType().getAircraftType();         //aircraft type
            historySummary[3][row] = r.getStartDate().toString();                //rental start date
            historySummary[4][row] = r.getEndDate().toString();                  //rental end date
            historySummary[5][row] = r.getStartTime().toString();               //rental end time
            historySummary[6][row] = r.getEndTime().toString();                 //rental start time
            historySummary[7][row] = (new TimeCalculator(r.getStartDate(), r.getEndDate(), r.getStartTime(), r.getEndTime())).toString(); //total time
            historySummary[8][row] = Long.toString(r.getReserver().getId());    //reserver id
            historySummary[9][row] = Double.toString(r.getPrice());             //rental charge
            row++;




        }
        return historySummary;
    }

    /**
     * Returns a 2 dimensions array containing all the rentals for a specific renter.
     * @param personID
     * @return renterReport
     */
    public String[][] getRenterReport(long personID) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = getReservationListPerPersonID(personID);
        String[][] renterReport = new String[10][reservationList.size()];
        int row = 0;
        for (Reservation r : reservationList) {
            renterReport[0][row] =
                    renterReport[1][row] = Long.toString(r.getId());      //rental id
            renterReport[2][row] = r.getRentalItem().getType();   // rental type
            renterReport[3][row] = r.getRentalItem().getAircraftType().getAircraftType();     //aircraft type

            if (r.getRentalItem().getType().equals("Airplane")) {           //if it's an airplane get the tail number
                Airplane plane = new Airplane();
                plane = (Airplane) r.getRentalItem();
                renterReport[4][row] = plane.getTailNumber();
            } else {                                                           // if rental item is not an airplane return N/A
                renterReport[4][row] = "N/A";
            }

            renterReport[5][row] = r.getStartDate().toString();       //rental start date
            renterReport[6][row] = r.getEndTime().toString();             //rental start time
            renterReport[7][row] = r.getStartTime().toString();      //rental start time
            renterReport[8][row] = r.getEndTime().toString();        // rental end time

            renterReport[9][row] = r.getPilot().getFirstName() + " " + r.getPilot().getLastName();
            row++;


        }

        return renterReport;
    }

    /**
     * Returns a string array containing all the informations needed to generate an airplane status report
     * @param airplaneID
     * @return statusReport
     */
    public String[] AircraftStatusReport(long airplaneID) {
        AirplaneDAO aDAO = AirplaneDAO.getInstance();
        Airplane airplane = new Airplane();
        try {
            airplane = (Airplane) aDAO.getSingle(airplaneID);
        } catch (SQLException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        }
        String[] statusReport = new String[10];
        statusReport[0] = Long.toString(airplane.getId());               //airplane id
        statusReport[1] = airplane.getTailNumber();                       //tail number
        statusReport[2] = airplane.getModel();                              // airplane model
        statusReport[3] = airplane.getAircraftType().getAircraftType();      //plane type
        statusReport[4] = Double.toString(airplane.getHobbsLastHundred());       //hobbs last hundred
        statusReport[5] = Double.toString(airplane.getHobbsLastOverhaul());         //hobbs last overhaul
        statusReport[6] = Double.toString(airplane.getHobbsLeftEngine());            //hobbs left engine
        statusReport[7] = Integer.toString(airplane.getLandings());                  //number of landings
        statusReport[8] = Integer.toString(airplane.getTakeOffs());                  // number of takeoffs

        return statusReport;
    }

    /**
     * Returns a string array containing all the informations needed to generate an airplane status report
     * @param airplane
     * @return statusReport
     */
    public String[] AircraftStatusReport(Airplane airplane) {

        return AircraftStatusReport(airplane.getId());
    }
}
